/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import Modelo.DAL.ConexionBD;
import Modelo.DAL.ReporteObjetoPerdido_DAO;
import Acceso.Security;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Rafael
 */
public class generarComprobanteOPAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Director DSI", "Jefe Apoyo Administrativo", "Personal Administrativo","Encargado Objetos Perdidos"};
       if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }
        System.out.println("Paso Autenticación");

        RegistrarEntregaOPForm formBean = (RegistrarEntregaOPForm) form;
        Long idObjeto = formBean.getIdObjeto();
        //int  id =  Integer.parseInt((String) request.getAttribute("id"));
        byte[] bytes = null;
        System.out.println("\n\n\n id:" + idObjeto);

        //src\java\Modulo4
        //JRResultSetDataSource jrs= null;
        /*
        JasperReport reporte = (JasperReport) JRLoader.loadObject("C:/Users/"
                + "Rafael/Documents/NetBeansProjects/trunk/SIGSIUSB/"
                + "Planillas/Constancia Entrega Objeto Perdido.jasper");

        File reportFile = new File("C:/Users/"
                + "Rafael/Documents/NetBeansProjects/trunk/SIGSIUSB/"
                + "Planillas/Constancia Entrega Objeto Perdido.jasper");
*/

        try {
            Map parametros = new HashMap();
            parametros.put("id_obj", idObjeto);
            System.out.println("El id objeto es: "+idObjeto);
            parametros.put("USBID", ReporteObjetoPerdido_DAO.findUSBID(idObjeto));
            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(getClass().getResource(
                    "/Modulo4/Constancia Entrega Objeto Perdido.jasper"));
            System.out.println("antes jasperRunManager");
            ConexionBD conn = new ConexionBD();
            bytes = JasperRunManager.runReportToPdf(jasperReport, parametros, conn.session);
            System.out.println("Longitud de bytes: "+bytes.length);

        } catch (Exception e) {
            System.out.println("Error " + e);
        }

        if (bytes != null && bytes.length > 0) {
            /*
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ServletOutputStream out = response.getOutputStream();

            bos.write(bytes);
             */
            response.setContentType("application/pdf");
            response.setHeader("Cache-Control", "cache");
            response.setHeader("Pragma", "cache");
            response.setHeader("Content-Disposition",
                    "attachment; filename=Comprobante_Entrega_Objeto_Perdido.pdf");

            response.setContentLength(bytes.length);

            ServletOutputStream outputStream = response.getOutputStream();

            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();

            return mapping.findForward(SUCCESS);
        }

        return mapping.findForward(FAILURE);
    }
}
